package com.oa.ylms.apps.dao.assess.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.ylms.apps.constants.assess.IAssessInfoStates;
import com.oa.ylms.apps.dao.assess.IAssessCheckDao;
import com.oa.ylms.apps.entity.archives.StaffInfoValue;
import com.oa.ylms.apps.entity.assess.AssessCheckValue;
import com.oa.ylms.apps.entity.assess.AssessInfoValue;
import com.oa.ylms.apps.entity.assess.AssessModelValue;

@SuppressWarnings("unchecked")
public class AssessCheckDaoImpl extends HibernateDaoSupport implements
		IAssessCheckDao {

	private List<AssessInfoValue> list;
	private int numbers;
	private List<AssessCheckValue> assessCheckList;
	
	public int add(AssessCheckValue assessCheck) throws Exception {
		if (assessCheck != null) {
			this.getHibernateTemplate().save(assessCheck);
			return assessCheck.getId();
		}
		return 0;
	}

	public int countTpagesByCheckList(StaffInfoValue staffInfo,
			final int yearNo, final int monthNo,
			final AssessInfoValue assessInfo,
			final AssessModelValue assessModel, int rows) throws Exception {
		int pageNumbers = 0;
		String hqlStr = "SELECT aiv FROM AssessInfoValue aiv, AssessModelValue amv where aiv.assessModel.modelId = amv.modelId and aiv.states = ?";
		if (yearNo != IAssessInfoStates.UNDEFINED) {
			hqlStr += " AND aiv.yearNo = ?";
		}
		if (monthNo != IAssessInfoStates.UNDEFINED) {
			hqlStr += " AND aiv.monthNo = ?";
		}
		if (assessModel.getModelName() != null) {
			hqlStr += " AND amv.modelName= ? ";
		}
		if (assessInfo.getAssessName() != null) {
			hqlStr += " AND aiv.assessName = ?";
		}
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = 0;
				query.setInteger(index, assessInfo.getStates());
				if (yearNo != IAssessInfoStates.UNDEFINED) {
					index++;
					query.setInteger(index, yearNo);
				}
				if (monthNo != IAssessInfoStates.UNDEFINED) {
					index++;
					query.setInteger(index, monthNo);
				}
				if (assessModel.getModelName() != null) {
					index++;
					query.setString(index, assessModel.getModelName());
				}
				if (assessInfo.getAssessName() != null) {
					index++;
					query.setString(index, assessInfo.getAssessName());
				}
				list = query.list();
				return list;
			}
		});
		this.numbers = list.size();
		if (numbers <= rows) {
			pageNumbers = 1;
		}
		if (numbers == 0) {
			pageNumbers = 0;
		} else {
			if (numbers % rows == 0) {
				pageNumbers = numbers / rows;
			} else {
				pageNumbers = numbers / rows + 1;
			}
		}
		return pageNumbers;
	}

	public int getTotalNumber() throws Exception {

		return numbers;
	}

	public List<AssessInfoValue> queryCheckList(StaffInfoValue staffInfo,
			final int yearNo, final int monthNo,
			final AssessInfoValue assessInfo,
			final AssessModelValue assessModel, final int rows, final int pageNo)
			throws Exception {
		String hqlStr = "SELECT aiv FROM AssessInfoValue aiv, AssessModelValue amv where aiv.assessModel.modelId = amv.modelId and aiv.states = ?";
		if (yearNo != IAssessInfoStates.UNDEFINED) {
			hqlStr += " AND aiv.yearNo = ?";
		}
		if (monthNo != IAssessInfoStates.UNDEFINED) {
			hqlStr += " AND aiv.monthNo = ?";
		}
		if (assessModel.getModelName() != null) {
			hqlStr += " AND amv.modelName= ? ";
		}
		if (assessInfo.getAssessName() != null) {
			hqlStr += " AND aiv.assessName = ?";
		}
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = 0;
				query.setInteger(index, assessInfo.getStates());
				if (yearNo != IAssessInfoStates.UNDEFINED) {
					index++;
					query.setInteger(index, yearNo);
				}
				if (monthNo != IAssessInfoStates.UNDEFINED) {
					index++;
					query.setInteger(index, monthNo);
				}
				if (assessModel.getModelName() != null) {
					index++;
					query.setString(index, assessModel.getModelName());
				}
				if (assessInfo.getAssessName() != null) {
					index++;
					query.setString(index, assessInfo.getAssessName());
				}
				query.setFirstResult(rows * (pageNo - 1));
				query.setMaxResults(rows);
				list = query.list();
				return list;
			}
		});
		return list;
	}

	public List<AssessCheckValue> queryAssessCheckByAssessId(final int assessId)
			throws Exception {
		String hqlStr = "select acv from AssessCheckValue acv where acv.assessInfo.assessId = ?";
		final String hql = hqlStr;	
		assessCheckList = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = 0;
				query.setInteger(index, assessId);
				assessCheckList = query.list();
				return assessCheckList;
			}
		});
		return assessCheckList;
	}

	public List<AssessInfoValue> getList() {
		return list;
	}

	public void setList(List<AssessInfoValue> list) {
		this.list = list;
	}

	public int getNumbers() {
		return numbers;
	}

	public void setNumbers(int numbers) {
		this.numbers = numbers;
	}

	public List<AssessCheckValue> getAssessCheckList() {
		return assessCheckList;
	}

	public void setAssessCheckList(List<AssessCheckValue> assessCheckList) {
		this.assessCheckList = assessCheckList;
	}

}
